const jwt = require("jsonwebtoken");
const { SECRET } = require("../config");
module.exports = function (req, res, next) {
  const token = req.get("token");

  if (!token) {
    return res.json({
      code: "2003",
      msg: "token为空",
      data: null,
    });
  }

  jwt.verify(token, SECRET, (err, data) => {
    if (err) {
      return res.json({
        code: "2001",
        msg: "token身份不合法",
        data: null,
      });
    }
    // 保存用户的信息
    req.user = data;
    next();
  });
};
